package com.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import com.annotation.IgnoreAuth;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.ShoufeixinxiEntity;
import com.entity.view.ShoufeixinxiView;
import com.service.DictionaryService;
import com.service.ShoufeixinxiService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 收费信息
 * 后端接口
 * @author
 * @email
*/
@RestController
@Controller
@RequestMapping("/shoufeixinxi")
public class ShoufeixinxiController {
    private static final Logger logger = LoggerFactory.getLogger(ShoufeixinxiController.class);

    @Autowired
    private ShoufeixinxiService shoufeixinxiService;


    @Autowired
    private TokenService tokenService;
    @Autowired
    private DictionaryService dictionaryService;





    //级联表service


    /**
    * 后端列表
    */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, HttpServletRequest request){
        logger.debug("page方法:,,Controller:{},,params:{}",this.getClass().getName(),JSONObject.toJSONString(params));
     
        String role = String.valueOf(request.getSession().getAttribute("role"));
        if(StringUtil.isNotEmpty(role) && "普通员工".equals(role)){
            params.put("yonghuId",request.getSession().getAttribute("userId"));
        }
        params.put("orderBy","id");
        PageUtils page = shoufeixinxiService.queryPage(params);

        //字典表数据转换
        List<ShoufeixinxiView> list =(List<ShoufeixinxiView>)page.getList();
        for(ShoufeixinxiView c:list){
            //修改对应字典表字段
            dictionaryService.dictionaryConvert(c);
        }
        return R.ok().put("data", page);
    }

    /**
    * 后端详情
    */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        logger.debug("info方法:,,Controller:{},,id:{}",this.getClass().getName(),id);
        ShoufeixinxiEntity shoufeixinxi = shoufeixinxiService.getById(id);
        if(shoufeixinxi !=null){
            //entity转view
            ShoufeixinxiView view = new ShoufeixinxiView();
            BeanUtils.copyProperties( shoufeixinxi , view );//把实体数据重构到view中

            //修改对应字典表字段
            dictionaryService.dictionaryConvert(view);
            return R.ok().put("data", view);
        }else {
            return R.error(511,"查不到数据");
        }

    }

    /**
    * 后端保存
    */
    @RequestMapping("/save")
    public R save(@RequestBody ShoufeixinxiEntity shoufeixinxi, HttpServletRequest request){
        logger.debug("save方法:,,Controller:{},,shoufeixinxi:{}",this.getClass().getName(),shoufeixinxi.toString());
        Wrapper<ShoufeixinxiEntity> queryWrapper = new QueryWrapper<ShoufeixinxiEntity>()
            .eq("mingmu_name", shoufeixinxi.getMingmuName())
            .eq("shoufeixinxi_types", shoufeixinxi.getShoufeixinxiTypes())
            ;
        logger.info("sql语句:"+queryWrapper.getSqlSegment());
        ShoufeixinxiEntity shoufeixinxiEntity = shoufeixinxiService.getOne(queryWrapper);
        if(shoufeixinxiEntity==null){
            shoufeixinxi.setInsertTime(new Date());
            shoufeixinxi.setCreateTime(new Date());
        //  String role = String.valueOf(request.getSession().getAttribute("role"));
        //  if("".equals(role)){
        //      shoufeixinxi.set
        //  }
            shoufeixinxiService.save(shoufeixinxi);
            return R.ok();
        }else {
            return R.error(511,"表中有相同数据");
        }
    }

    /**
    * 后端修改
    */
    @RequestMapping("/update")
    public R update(@RequestBody ShoufeixinxiEntity shoufeixinxi, HttpServletRequest request){
        logger.debug("update方法:,,Controller:{},,shoufeixinxi:{}",this.getClass().getName(),shoufeixinxi.toString());
        //根据字段查询是否有相同数据
        Wrapper<ShoufeixinxiEntity> queryWrapper = new QueryWrapper<ShoufeixinxiEntity>()
            .notIn("id",shoufeixinxi.getId())
            .eq("mingmu_name", shoufeixinxi.getMingmuName())
            .eq("shoufeixinxi_types", shoufeixinxi.getShoufeixinxiTypes())
            ;
        logger.info("sql语句:"+queryWrapper.getSqlSegment());
        ShoufeixinxiEntity shoufeixinxiEntity = shoufeixinxiService.getOne(queryWrapper);
        if(shoufeixinxiEntity==null){
            //  String role = String.valueOf(request.getSession().getAttribute("role"));
            //  if("".equals(role)){
            //      shoufeixinxi.set
            //  }
            shoufeixinxiService.updateById(shoufeixinxi);//根据id更新
            return R.ok();
        }else {
            return R.error(511,"表中有相同数据");
        }
    }



    /**
    * 删除
    */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        logger.debug("delete:,,Controller:{},,ids:{}",this.getClass().getName(),ids.toString());
        shoufeixinxiService.removeBatchByIds(Arrays.asList(ids));
        return R.ok();
    }



    //导出
    @GetMapping("/exportAll")
    @IgnoreAuth
    public void exportFile(){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            shoufeixinxiService.export();
        } catch (IOException e) {
            System.out.println("导出失败");
            e.printStackTrace();
        }
        stopWatch.stop();
        System.out.println("共计耗时： " + stopWatch.getTotalTimeSeconds()+"S");
    }




}

